/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.dao;

import com.bluemartini.dna.BMException;

/**
 * Thrown to indicate that an unrecoverable data
 * access error has occurred.
 * 
 * <p>
 * This is a run-time extension primary so that user code
 * does not have to be responsible for catching data access
 * errors.
 * </p>
 * 
 * <p>
 * More information on the motivation behind MartiniMix's use
 * of unchecked exceptions can be found
 * <a href="http://www.martinimix.net/docs/martinimix/chapter-foo">here</a>
 * </p>
 * 
 * @author Scott Rossillo
 *
 */
public class DataAccessException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	
	private static Throwable decipherActualCause(Throwable cause) {
		
		Throwable t = cause.getCause();
		
		if(t != null) {
			if(t instanceof BMException ) {
				return decipherActualCause(t);
			} else {
				return t;
			}
		}
		
		return cause;
	}
	
	/**
	 * Constructs a new data access exception from the given message and cause.
	 * 
	 * @param message
	 * @param cause
	 */
	public DataAccessException(String message, Throwable cause) {
		super(message, decipherActualCause(cause));
	}
	
	/**
	 * Constructs a new data access exception from the given message and
	 * Blue Martini cause.  Since Blue Martini insists on throwing a checked
	 * generic exception, we'll help them out by finding the actual cause.
	 * 
	 * @param message
	 * @param cause
	 */
	public DataAccessException(String message, BMException cause) {
		super(message, decipherActualCause(cause));
	}
	

	public DataAccessException(String message) {
		super(message);
	}

	public DataAccessException(Throwable cause) {
		super(cause);
	}
}
